<?php
class Photo extends Vfriend_Db {
	var $cache;
	var $clear;
	var $behaviour;
	var $notify;
	var $mlogs;
	public function __construct()
	{
		$this->cache 		= new Vfriend_Cache();
		$this->clear 		= $this->cache->clear;
		$this->behaviour	= new Behaviour();
		$this->notify		= new Notification();
		$this->mlogs		= new Mlogs();
	}
	public function getPhoto($intUserId, $intImageId) {
		try {
			$strKey 	= PHOTO_GET . $intUserId . $intImageId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call images_get(?,?)');
				$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
				$this->bindParam(2, $intImageId, 	PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PHOTO_GET_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getPhoto - ' .$e->getMessage());			
			return null;
		}
	}
	public function getPhotoByAlbum($intAlbumId, $intType, $intOff, $intLimit, &$intTotal=0) {
		try {
			$strKey 	= PHOTO_GETPHOTOBYALBUM . $intAlbumId . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call images_getall_album(?,?,?,?)');
				$this->bindParam(1, $intAlbumId, 	PDO::PARAM_INT, 11);
				$this->bindParam(2, $intType, 		PDO::PARAM_INT, 4);
				$this->bindParam(3, $intOff, 		PDO::PARAM_INT, 4);
				$this->bindParam(4, $intLimit, 		PDO::PARAM_INT, 4);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PHOTO_GET_EXPIRE);
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getPhotoByAlbum - ' .$e->getMessage());			
			return null;
		}
	}
	public function add($intUserId, $strPath, $intAlbumId=0, $strNote='', $intType=1) {
		try {
			$this->prepare('call images_add(?,?,?,?,?)');
			$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intAlbumId, 	PDO::PARAM_INT, 11);
			$this->bindParam(3, $strPath, 		PDO::PARAM_STR, 150);
			$this->bindParam(4, $strNote, 		PDO::PARAM_STR, 255);
			$this->bindParam(5, $intType, 		PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intImageId	= isset($arrResult[0]['imageid']) ? $arrResult[0]['imageid'] : 0;
			if($intImageId) {
				$this->clear->deletePhotoGet($intUserId,$intImageId);
				$this->clear->deleteAlbumByUserId($intUserId,$intAlbumId);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::add - ' .$e->getMessage());			
			return null;
		}
	}
	public function addForAlbum($intUserId, $intAlbumId, $strName, $intPerm, $strPath,&$intImageId=0) {
		try {
			$this->prepare('call images_add_album(?,?,?,?,?)');
			$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intAlbumId, 	PDO::PARAM_INT, 11);
			$this->bindParam(3, $strName, 		PDO::PARAM_STR, 150);
			$this->bindParam(4, $intPerm, 		PDO::PARAM_STR, 255);
			$this->bindParam(5, $strPath, 		PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intImageId	= isset($arrResult[0]['imageid']) ? $arrResult[0]['imageid'] : 0;
				$intAlbumId	= isset($arrResult[0]['albumid']) ? $arrResult[0]['albumid'] : 0;
				if($strName == '') {
					$strName	= isset($arrResult[0]['name']) ? $arrResult[0]['name'] : '';
				}
				$this->clear->deletePhotoGet($intUserId,$intImageId);
				$this->clear->deleteAlbumByUserId($intUserId,$intAlbumId);
				if($intPerm < 2) {
					$objVfriendUser	= new Vfriend_User();
					$arrRelation	= $objVfriendUser->getRelation($intUserId,2);
					if($arrRelation) {
						$strContent = Zend_Json::encode(
											array(	'posterid'=>$intUserId,
													'imageid'=>$intImageId
												)
									);
						$strJson	= Zend_Json::encode(
											array(
													'posterid'		=> $intUserId,
													'albumid'		=> $intAlbumId,
													'album_name'	=> $strName,
													'path'			=> $strPath,
													'imageid'		=> $intImageId
											)
									);
						foreach($arrRelation as $value) {
							$this->notify->insert($value,44,$strContent,$intImageId);
							$this->logBehaviour($value,$intUserId,$intUserId,21,$intAlbumId);
							$this->mlogs->insert($value,40,$strJson);
						}
					}
					$this->logBehaviour($intUserId,$intUserId,$intUserId,21,$intAlbumId);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::addForAlbum - ' .$e->getMessage());			
			return null;
		}
	}
	public function update($intUserId, $intImageId, $intAlbumId, $strPath, $strNote='', $intType=1) {
		try {
			$this->prepare('call images_update(?,?,?,?,?)');
			$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intImageId, 	PDO::PARAM_INT, 4);
			$this->bindParam(3, $intAlbumId, 	PDO::PARAM_INT, 11);			
			$this->bindParam(4, $strPath, 		PDO::PARAM_STR, 150);
			$this->bindParam(5, $strNote, 		PDO::PARAM_STR, 255);			
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deletePhotoGet($intUserId,$intImageId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::update - ' .$e->getMessage());			
			return null;
		}
	}
	public function delete($intUserId, $intImageId) {
		try {
			$this->prepare('call images_delete(?,?)');
			$this->bindParam(1, $intUserId, 	PDO::PARAM_INT, 11);
			$this->bindParam(2, $intImageId, 	PDO::PARAM_INT, 4);		
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$intAlbumId = isset($arrResult[0]['albumid']) ? $arrResult[0]['albumid'] : 0;
				$strPath	= isset($arrResult[0]['path']) ? $arrResult[0]['path'] : '';
				$this->clear->deletePhotoGet($intUserId,$intImageId);
				$this->clear->deleteCmtForPhoto($intImageId);
				$this->clear->deleteAlbumByUserId($intUserId, $intAlbumId);
				$this->clear->deleteUser($intUserId);
				$this->unlinkImg(0,0,$strPath);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::delete - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAlbumInfo($intAlbumId) {
		try {
			$strKey 	= PHOTO_GETALBUMINFO . $intAlbumId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call ialbums_get(?)');
				$this->bindParam(1, $intAlbumId, PDO::PARAM_INT, 11);
				$arrData 	= $this->fetchAll();
				$this->cache->set($strKey,$arrData,PHOTO_GETALBUMINFO_EXPIRE);
			}
			return isset($arrData[0]) ? $arrData[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getAlbumInfo - ' . $e->getMessage());
			return null;
		}
	}
	public function updateAlbum($intUserId, $intAlbumId, $strName, $intPerm) {
		try {
			$this->prepare('call ialbums_update(?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intAlbumId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strName, PDO::PARAM_INT, 150);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			if($this->execute()) {
				$this->clear->deleteAlbumByUserId($intUserId,$intAlbumId);
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getAlbumInfo - ' . $e->getMessage());
			return null;
		}
	}
	public function updateAlbumPerm($intAlbumId, $intPerm, &$intOwnerId=0) {
		try {
			$this->prepare('call ialbums_update_perm(?,?)');
			$this->bindParam(1, $intAlbumId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPerm, PDO::PARAM_INT, 4);
			$arrResult = $this->fetchAll();
			if($arrResult) {
				$intOwnerId = isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
				$this->clear->deleteAlbumByUserId($intOwnerId,$intAlbumId);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::updateAlbumPerm - ' . $e->getMessage());
			return null;
		}
	}
	public function deleteAlbum($intUserId, $intAlbumId) {
		try {
			$this->prepare('call ialbums_delete(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intAlbumId, PDO::PARAM_INT, 11);
			$arrResult	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				if($arrResult) {
					foreach($arrResult as $value) {
						$this->unlinkImg(0,0,$value['path']);
						$this->clear->deleteCmtForPhoto($value['imageid']);
						$this->clear->deletePhotoGet($intUserId,$value['imageid']);
					}
				}
				$this->clear->deleteAlbumByUserId($intUserId,$intAlbumId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::deleteAlbum - ' . $e->getMessage());
			return null;
		}
	}
	public function getAlbumByUserId($intUserId, $intType=1) {
		try {
			$strKey 	= PHOTO_GETALBUMBYUSERID . $intUserId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call ialbums_get_user(?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrData 	= $this->fetchAll();
				$this->cache->set($strKey, $arrData, PHOTO_GETALBUMBYUSERID_EXPIRE);
			}
			if($intType == 1) {
				foreach($arrData as $value) {
					$arrResult[$value['albumid']] = $value['name'];
				}
				return $arrResult;
			}
			return $arrData;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getAlbumByUserId - ' . $e->getMessage());
			return null;
		}
	}
	public function getAlbum($intUserId, $intType, $intOffset, $intLimit,&$intTotal=0) {
		try {			
			$arrResult		= null;
			if($intOffset == 0) {
				$strKey 	= PHOTO_GETALBUM . $intUserId . $intType;
				$arrResult	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ialbums_getall(?,?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);
				$this->bindParam(3, $intOffset, PDO::PARAM_INT, 11);
				$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);
				$arrResult 	= $this->fetchAll();
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, PHOTO_GETALBUM_EXPIRE);
				}
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getAlbum - ' . $e->getMessage());
			return null;
		}
	}
	public function getImageByAlbumId($intUserId, $intAlbumId, $intLimit,&$intTotal=0) {
		try {
			$arrResult	= null;
			$strKey 	= PHOTO_GETIMAGEBYALBUMID . $intUserId . $intAlbumId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call images_getall(?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intAlbumId, PDO::PARAM_INT, 11);
				$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PHOTO_GETIMAGEBYALBUMID_EXPIRE);
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getImageByAlbumId - ' . $e->getMessage());
			return null;
		}
	}
	/**$intTime default 1 hour*/
	public function getImageForAlbum($intAlbumId,$intTime=1) {
		try {
			$arrResult	= null;
			$strKey 	= PHOTO_GETIMAGEFORALBUM . $intAlbumId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call images_getall_topnews(?,?)');
				$this->bindParam(1, $intAlbumId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intTime, PDO::PARAM_INT, 4);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PHOTO_GETIMAGEFORALBUM_EXPIRE);
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getImageForAlbum - ' . $e->getMessage());
			return null;
		}
	}
	public function getCmtTotal($intImageId) {
		try {
			$strKey 	= PHOTO_GETCMTTOTAL . $intImageId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call icomments_get_total(?)');
				$this->bindParam(1, $intImageId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PHOTO_GETCMTTOTAL_EXPIRE);
			}
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;			
			return $intTotal;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getCmtTotal - ' . $e->getMessage());
			return null;
		}
	}
	public function getCmtAll($intImageId, $intOff, $intLimit) {
		try {
			$this->prepare('call icomments_getall(?,?,?)');
			$this->bindParam(1, $intImageId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getCmtAll - ' . $e->getMessage());
			return null;
		}
	}
	public function getTopCmt($intImageId, $intLimit=BEHAVIOUR_TOP_LIMIT) {
		$strKey 	= PHOTO_GETTOPCMT . $intImageId;
		$arrResult	= $this->cache->get($strKey);		
		if(!Vfriend_Function::isCached($arrResult)) {
			$this->prepare('call icomments_get_top(?,?)');
			$this->bindParam(1, $intImageId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();						
			$this->cache->set($strKey, $arrResult, PHOTO_GETTOPCMT_EXPIRE);
		}
		return $arrResult;
	}
	public function getCmtUser($intImageId) {
		try {	
			$strKey 	= PHOTO_GETCMTUSER . $intImageId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call icomments_get_user(?)');
				$this->bindParam(1, $intImageId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();			
				$this->cache->set($strKey, $arrResult, PHOTO_GETCMTUSER_EXPIRE);
			}
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getCmtUser - ' .$e->getMessage());			
			return null;
		}
	}
	public function getQuote($intImageId=0, $intType=0) {
		try {	
			$strKey 	= PHOTO_GETQUOTE . $intImageId . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call images_get_quote(?,?)');
				$this->bindParam(1, $intImageId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();			
				$this->cache->set($strKey, $arrResult, PHOTO_GETQUOTE_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getQuote - ' .$e->getMessage());			
			return null;
		}
	}	
	public function addComment($intUserId, $intImageId, $strContent,&$intOwnerId=0) {
		try {
			$this->prepare('call icomments_add(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intImageId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strContent, PDO::PARAM_STR);
			$arrResult	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			$intOwnerId	= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
			if($intError == 0) {
				$intAlbumId		= isset($arrResult[0]['albumid']) ? $arrResult[0]['albumid'] : 0;				
				$strAlbumName	= isset($arrResult[0]['name']) ? $arrResult[0]['name'] : '';				
				$strPath		= isset($arrResult[0]['path']) ? $arrResult[0]['path'] : '';				
				$intCmtId		= isset($arrResult[0]['icommentid']) ? $arrResult[0]['icommentid'] : 0;
				$this->clear->deleteCmtForPhoto($intImageId);
				$this->clear->deleteAlbumByUserId($intOwnerId,$intAlbumId);
				$this->clear->deleteUstat($intUserId);
				$arrUser	= $this->getCmtUser($intImageId);				
				if($arrUser) {					
					$isHasOwner 	= 0;
					$strContentJson = Zend_Json::encode(
														array(	'posterid'		=> $intUserId,
																'ownerid'		=> $intOwnerId,
																'imageid'		=> $intImageId,
																'albumid'		=> $intAlbumId,
																'album_name'	=> $strAlbumName,
																'path'			=> $strPath,
																'cmt'			=> $strContent
															)
													);
					foreach($arrUser as $value) {
						$intReceiveId = $value['userid'];
						if($intReceiveId == $intOwnerId) {
							$isHasOwner++;
						}
						if($intUserId != $intReceiveId) {
							$this->notify->insert($intReceiveId,50,$strContentJson,$intCmtId);
							$this->mlogs->insert($intReceiveId,41,$strContentJson);
						}
						$this->logBehaviour($intReceiveId, $intOwnerId, $intUserId, 22, $intImageId);	
					}
					if(!$isHasOwner && $intUserId != $intOwnerId) {
						$this->notify->insert($intOwnerId,50,$strContentJson,$intCmtId);
						$this->mlogs->insert($intOwnerId,41,$strContentJson);
					}
				}
				$this->logBehaviour($intOwnerId, $intOwnerId, $intUserId, 22, $intImageId);
				$this->logBehaviour($intUserId, $intOwnerId, $intUserId, 22, $intImageId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::addComment - ' . $e->getMessage());
			return null;
		}
	
	}
	public function deleteCmt($intUserId, $intImageId, $intIcommentId) {
		try {
			$this->prepare('call icomments_delete(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intImageId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intIcommentId, PDO::PARAM_INT, 11);
			$arrResult	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intAlbumId	= isset($arrResult[0]['albumid']) ? $arrResult[0]['albumid'] : 0;
				$intOwnerId	= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
				$this->clear->deleteAlbumByUserId($intOwnerId,$intAlbumId);
				$this->clear->deleteCmtForPhoto($intImageId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::deleteCmt - ' . $e->getMessage());
			return null;
		}	
	}
	public function getLike($intImageId, $intLimit=BEHAVIOUR_TOP_LIMIT) {
		try {
			$strKey	 	= PHOTO_GETLIKE . $intImageId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ilikes_get_top(?,?)');
				$this->bindParam(1, $intImageId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PHOTO_GETLIKE_EXPIRE);				
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::getLike - ' . $e->getMessage());
			return null;
		}
	}
	public function addLike($intUserId, $intImageId, &$intLike=0) {
		try {
			$this->prepare('call ilikes_add(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intImageId, PDO::PARAM_INT, 11);
			$arrResult	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intLike = isset($arrResult[0]['like']) ? $arrResult[0]['like'] : 0;
				$intAlbumId	= isset($arrResult[0]['albumid']) ? $arrResult[0]['albumid'] : 0;
				$intOwnerId	= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
				$this->clear->deleteAlbumByUserId($intOwnerId,$intAlbumId);
				$this->clear->deleteCmtForPhoto($intImageId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::addLike - ' . $e->getMessage());
			return null;
		}	
	}
	public function addLikeCmt($intUserId, $intImageId, $intIcommentId, &$intLike=0) {
		try {
			$this->prepare('call iclikes_add(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intImageId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intIcommentId, PDO::PARAM_INT, 11);
			$arrResult	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intLike 		= isset($arrResult[0]['tlike']) ? $arrResult[0]['tlike'] : 0;
				$$intOwnerId 	= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
				$this->clear->deleteCmtForPhoto($intImageId);
				$arrUser	= $this->getCmtUser($intImageId);				
				if($arrUser) {
					$isHasOwner = 0;
					$strContentJson = Zend_Json::encode(
														array(	'posterid'	=> $intUserId,
																'ownerid'	=> $intOwnerId,
																'imageid'	=> $intImageId,
																'cmt'		=> $strContent
															)
													);
					foreach($arrUser as $value) {
						$intReceiveId = $value['userid'];
						if($intReceiveId == $intOwnerId) {
							$isHasOwner++;
						}
						if($intUserId != $intReceiveId) {
							$this->notify->insert($intReceiveId,50,$strContentJson,$intCmtId);
						}
						$this->logBehaviour($intReceiveId, $intOwnerId, $intUserId, 23, $intImageId);	
					}
					if(!$isHasOwner) {
						$this->notify->insert($intOwnerId,50,$strContentJson,$intCmtId);
						// $this->logBehaviour($intOwnerId, $intOwnerId, $intUserId, 23, $intImageId);
					}
				}
				// $this->logBehaviour($intUserId, $intOwnerId, $intUserId, 23, $intImageId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Photo::addLikeCmt - ' . $e->getMessage());
			return null;
		}
	
	}
	
	public function unlinkImg($intUserId, $intImageId, $strPath='') {
		$arrImage = $this->getPhoto($intUserId, $intImageId);
		if($strPath == '') {
			if($arrImage) {
				$strFull 	= '';
				$strThumb 	= '';
				$strSmall	= '';
				if($arrImage['type'] == 1) {
					$strFull 	= DIR_AVATAR_FULL . $arrImage['path'];
					$strThumb	= DIR_AVATAR_THUMB . $arrImage['path'];				
					$strSmall	= DIR_AVATAR_SMALL . $arrImage['path'];				
				}
				elseif($arrImage['type'] == 2) {
					$strFull 	= DIR_ALBUM_FULL . $arrImage['path'];
					$strThumb	= DIR_ALBUM_THUMB . $arrImage['path'];		
				}
				if(file_exists($strFull))
					@unlink($strFull);
				if(file_exists($strThumb))
					@unlink($strThumb);
				if(file_exists($strSmall))
					@unlink($strSmall);
			}
		} else {
			$strFull 	= DIR_ALBUM_FULL . $strPath;
			$strThumb	= DIR_ALBUM_THUMB . $strPath;
			$strAFull 	= DIR_AVATAR_FULL . $strPath;
			$strAThumb	= DIR_AVATAR_THUMB . $strPath;				
			$strASmall	= DIR_AVATAR_SMALL . $strPath;		
			if(file_exists($strFull))
				@unlink($strFull);
			if(file_exists($strThumb))
				@unlink($strThumb);
			if(file_exists($strAFull))
				@unlink($strAFull);
			if(file_exists($strAThumb))
				@unlink($strAThumb);
			if(file_exists($strASmall))
				@unlink($strASmall);
		}
	}
	public function logBehaviour($intUserId,$intOwnerId,$intPosterId,$intType,$intSrcId) {
		$strQueryCore 		= $intType == 21 ? 'getImageForAlbum' : 'getPhoto';
		$strQueryLike		= 'getLike';
		$strQueryTotalCmt	= 'getCmtTotal';
		$strQueryCmt		= 'getTopCmt';		
		$strQueryQuote		= 'getQuote';
		$this->behaviour->insert($intUserId,
								 $intOwnerId,
								 $intPosterId,
								 $strQueryCore,
								 $strQueryLike,
								 $strQueryTotalCmt,
								 $strQueryCmt,
								 $strQueryQuote,
								 $intType,
								 $intSrcId);
	}
}
?>